package com.example.commonlib.base.view;



import com.example.commonlib.base.annotation.LayoutRes;
import com.example.commonlib.base.annotation.SuppressLint;
import com.example.commonlib.base.presenter.RequirePresenter;
import com.example.commonlib.base.presenter.SuperPresenter;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.jetbrains.annotations.Nullable;

import java.lang.annotation.Annotation;
import java.util.Arrays;


/**
 * <p>
 * MVP模型中把Fragment作为view层，可通过getPresenter()调用对应的presenter实例
 * <p>
 * Created by linlongxin on 2016/8/6.
 */

public class SuperFraction<T extends SuperPresenter> extends BaseFraction {
    private final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,0x00201,"TEST001");

    private final String TAG = getClass().getSimpleName();

    private T mPresenter;

    public SuperFraction() {
    }

    @SuppressLint("ValidFragment")
    public SuperFraction(int layoutResID) {
        super(layoutResID);
    }

    @SuppressLint("ValidFragment")
    public SuperFraction(@LayoutRes int layoutResID, boolean isUseStatusPages) {
        super(layoutResID, isUseStatusPages);
    }

    @Override
    public void onStart(Intent intent) {
        HiLog.error(label, "*****************SuperFraction onStart******************");
        super.onStart(intent);
    }

    // onCreateView 之后回调
    @Nullable
    @Override
    public Component onComponentAttached(LayoutScatter scatter, @Nullable ComponentContainer container, @Nullable Intent intent) {
        attachPresenter();
        Component component = super.onComponentAttached(scatter, container, intent);
        HiLog.error(label, "*****************SuperFraction onComponentAttached******************" + mPresenter);
        onInitViews();
        if (mPresenter != null) {
            mPresenter.onCreate(intent);
        }
        return component;
    }

    @Override
    public void onActive() {
        super.onActive();
        if (mPresenter != null) {
            mPresenter.onActive();
        }
    }

    private void attachPresenter() {
        if (mPresenter != null) {
            return;
        }
        Annotation[] annotations = getClass().getAnnotations();
        if (annotations.length > 0) {
            for (Annotation annotation : annotations) {
                if (annotation instanceof RequirePresenter) {
                    RequirePresenter presenter = (RequirePresenter) annotation;
                    try {
                        HiLog.error(label, "*****************SuperFraction try******************: " + annotations.length);
                        mPresenter = (T) presenter.value().newInstance();
                        mPresenter.attachView(this);
                    } catch (Exception e) {
                        HiLog.error(label, "*****************SuperFraction try e ******************: " + e);
                        HiLog.error(label, "*****************SuperFraction try e ******************: " + Arrays.toString(e.getStackTrace()));
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public T getPresenter() {
        attachPresenter();
        return mPresenter;
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mPresenter != null) {
            mPresenter.onStop();
        }
        mPresenter = null;
    }

    @Override
    public void onInitViews() {
        HiLog.error(label, "*****************SuperFraction onInitViews******************");
    }

    @Override
    public void onErrorRetry(Component v) {
        showLoading();
    }
}
